//
//  LikeDropsVC.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/19.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit
import Dialog

class LikeDropCell: DropListCell {
    @IBOutlet weak var removeBtn: UIButton!
}

class LikeDropsVC: DropListVC {
    @IBOutlet weak var navEditBtn: UIButton!
    

    override var drops: [Drop]!{
        didSet{
            title = "收藏 (\(drops.count))"
        }
    }
    
    fileprivate var isRemoveEditing = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func cellDelete(_ sender: UIButton){
        if let cell = sender.superview?.superview as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) {
//            let drop = drops[indexPath.row]
			
			func confirmDelete(){
				drops.remove(at: indexPath.row)
				tableView.deleteRows(at: [indexPath], with: .automatic)
				navEditBtn.isHidden = drops.isEmpty
			}
			
			Dialog.default(title: .init("确定要删除该条墨滴?"), message: .init("删除后不可恢复"), actions:[
				.init(title: "删除", style: .destructive, handler: {_ in
					confirmDelete()
				}),
				.init(title: "取消", style: .cancel)
			])
        }
    }
    
    @IBAction func navEdit(_ sender: UIButton) {
        isRemoveEditing = !isRemoveEditing
        let icon = isRemoveEditing ? "check_54" : "edit"
        sender.setImage(UIImage.named(icon), for: .normal)
        tableView.reloadSectionsWithoutAnimation(.zero)
    }
    
    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueCell(withClass: LikeDropCell.self, for: indexPath)!
        
        cell.drop = drops[indexPath.row]
        cell.removeBtn.isHidden = !isRemoveEditing

        return cell
    }

}
